package com.example.codereview.editor;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

/**
 * @author xianfeng
 * @date 2024/11/7 19:37
 * @description:
 */
public class EditorAreaIllustration extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        // Get access to the editor and caret model.
        Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
        CaretModel caretModel = editor.getCaretModel();

        // Getting the primary caret ensures we get the correct one of a possible many.
        Caret primaryCaret = caretModel.getPrimaryCaret();
        // Get the caret information
        LogicalPosition logicalPos = primaryCaret.getLogicalPosition();
        VisualPosition visualPos = primaryCaret.getVisualPosition();
        int caretOffset = primaryCaret.getOffset();

        // Build and display the caret report.
        String report = logicalPos.toString() + "\n" +
                visualPos.toString() + "\n" +
                "Offset: " + caretOffset;
        Messages.showInfoMessage(report, "Caret Parameters Inside The Editor");
    }
}
